<?php

namespace app\controller;

use app\model\AfterSale;
use app\model\Attachment;
use support\Request;

class AfterSaleController extends BaseController
{

    public function lists()
    {
        $list =  AfterSale::query()->orderBy('id', 'desc')->get();
        return $this->success($list);
    }

    public function create(Request $request)
    {

        // var_dump($this->getUserId($request));
        $afterSale = AfterSale::query()
            ->create([
                'sn' => $request->post('sn'),
                'channel' => $request->post('channel'),
                'door_date' => $request->post('door_date'),
                'start_time' => $request->post('start_time'),
                'end_time' => $request->post('end_time'),
                'desc' => $request->post('desc'),
                'user_id' => $this->getUserId($request),
            ]);


        $attachments =  $request->post('img_src');

        $jsonString = json_decode($attachments, 1);

        if (is_array($jsonString)) {

            foreach ($jsonString as $attach) {
                Attachment::create([
                    'path' => $attach,
                    'after_sale_id' => $afterSale->id,
                ]);
            }
        }

        return $this->success($afterSale);
    }
}
